﻿using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Web;

namespace wojilu
{
    public static class UrlOper
    {
        #region 返回指定的服务器变量信息
        /// <summary>
        /// 返回指定的服务器变量信息
        /// </summary>
        /// <param name="strName">服务器变量名</param>
        /// <returns>服务器变量信息</returns>
        public static string GetServerString(string strName)
        {
            if (HttpContext.Current.Request.ServerVariables[strName] == null)
                return "";

            return HttpContext.Current.Request.ServerVariables[strName].ToString();
        }
        #endregion

        #region 获取服务器端信息
        public static string ServerDep(int type)
        {
            switch (type)
            {
                case 0: //返回服务器IP
                    return UrlOper.GetServerString("LOCAL_ADDR");

                case 1: //返回服务器名称
                    return System.Web.HttpContext.Current.Server.MachineName;

                case 2: //返回服务器系统名称
                    {
                        OperatingSystem Sys = Environment.OSVersion;
                        return Sys.Platform.ToString();
                    }

                case 3: //返回服务器系统版本
                    return Environment.OSVersion.ToString();

                case 4: //获得服务器系统的标识
                    {
                        OperatingSystem Sys = Environment.OSVersion;
                        return Sys.Version.ToString();
                    }

                case 5: //返回服务器端口号
                    return UrlOper.GetServerString("SERVER_PORT");

                case 6: //返回服务器IIS版本
                    return UrlOper.GetServerString("SERVER_SOFTWARE");

                case 7: //返回服务器文件的路径 获取IIS物理路径
                    return UrlOper.GetServerString("APPL_PHYSICAL_PATH");

                case 8: //返回当前页面的虚拟路径
                    return System.Web.HttpContext.Current.Request.CurrentExecutionFilePath;

                case 9: //获得服务器框架版本
                    int build, major, minor, revision;
                    build = Environment.Version.Build;
                    major = Environment.Version.Major;
                    minor = Environment.Version.Minor;
                    revision = Environment.Version.Revision;
                    return major + "." + minor + "." + build + "." + revision;

                case 10: //获得脚本超时的时间
                    return System.Web.HttpContext.Current.Server.ScriptTimeout.ToString() + "秒";

                case 11: //获取服务器域名,带端口号
                    return UrlOper.GetServerString("HTTP_HOST").ToString();

                case 12: //获取服务器域名,不带端口号
                    return UrlOper.GetServerString("SERVER_NAME").ToString();

                case 13: //获取系统所在文件夹
                    return Environment.SystemDirectory.ToString();

                case 14: //获取服务器的语言种类
                    return CultureInfo.InstalledUICulture.EnglishName;

                case 15: //获取服务器IE版本
                    RegistryKey key = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Internet Explorer\Version Vector");
                    return key.GetValue("IE", "未检测到").ToString();

                case 16: //获取服务器上次启动到现在已运行
                    return (((Environment.TickCount / 0x3e8) / 60)).ToString() + "分钟";

                case 17: //获取逻辑驱动器
                    var str = string.Empty;
                    string[] logicalDrives = Directory.GetLogicalDrives();
                    for (int i = 0; i < (logicalDrives.Length - 1); i++)
                    {
                        str = str + logicalDrives[i].ToString();
                    }
                    return str;

                case 18: //获取CPU 总数
                    return Environment.GetEnvironmentVariable("NUMBER_OF_PROCESSORS").ToString();

                case 19: //获取CPU 类型
                    return Environment.GetEnvironmentVariable("PROCESSOR_IDENTIFIER").ToString();

                case 20: //获取虚拟内存
                    return ((Environment.WorkingSet / 0x400L)).ToString() + "M";

                case 21: //获取当前程序占用内存
                    return ((((double)GC.GetTotalMemory(false)) / 1048576.0)).ToString("N2") + "M";

                case 22: //获取Asp.net所占内存
                    return ((((double)Process.GetCurrentProcess().WorkingSet64) / 1048576.0)).ToString("N2") + "M";

                case 23: //获取Asp.net所占CPU
                    return Process.GetCurrentProcess().TotalProcessorTime.TotalSeconds.ToString("N0");

                case 24: //获取当前Session数量
                    return HttpContext.Current.Session.Contents.Count.ToString();

                case 25: //获取当前SessionID
                    return HttpContext.Current.Session.Contents.SessionID;

                case 26: //获取当前系统用户名
                    return Environment.UserName;

            }
            return string.Empty;
        }
        #endregion

        #region 获取客户端信息
        public static string clientBrower(int type)
        {
            switch (type)
            {
                case 0: //返回浏览器操作系统名称
                    return System.Web.HttpContext.Current.Request.Browser.Platform.ToString();

                case 1: //返回浏览器IP
                    return System.Web.HttpContext.Current.Request.UserHostAddress;
                //return System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];//此方法也可以

                case 2: //返回浏览器是否支持JavaScript
                    return (System.Web.HttpContext.Current.Request.Browser.EcmaScriptVersion.Major > 1).ToString();

                case 3: //返回浏览器是否支持VBScript
                    return System.Web.HttpContext.Current.Request.Browser.VBScript.ToString();

                case 4: //返回浏览器型号
                    return System.Web.HttpContext.Current.Request.Browser.Browser.ToString();

                case 5: //返回浏览器版本号
                    return System.Web.HttpContext.Current.Request.Browser.Version.ToString();

                case 6: //返回浏览器.NET版本
                    return System.Web.HttpContext.Current.Request.Browser.ClrVersion.ToString();

                case 7: //返回浏览器是否支持Cookies
                    return System.Web.HttpContext.Current.Request.Browser.Cookies.ToString();

                case 8: //返回浏览器是否支持Frame
                    return System.Web.HttpContext.Current.Request.Browser.Frames.ToString();

                case 9: //返回浏览器是否支持ActiveX
                    return System.Web.HttpContext.Current.Request.Browser.ActiveXControls.ToString();

                case 10: //返回浏览器是否支持Dom
                    return System.Web.HttpContext.Current.Request.Browser.MSDomVersion.ToString();

                case 11: //返回浏览器是否支持JavaApplets
                    return System.Web.HttpContext.Current.Request.Browser.JavaApplets.ToString();

                case 12: //操作系统的默认语言
                    return UrlOper.GetServerString("HTTP_ACCEPT_LANGUAGE");

                case 13: //返回浏览器IE版本
                    return System.Web.HttpContext.Current.Request.Browser.Browser.ToString() + System.Web.HttpContext.Current.Request.Browser.Version.ToString();

                default:
                    return string.Empty;

            }
        }
        #endregion

        #region 获取当前请求的原始 URL(URL 中域信息之后的部分,包括查询字符串(如果存在))
        /// <summary>
        /// 获取当前请求的原始 URL(URL 中域信息之后的部分,包括查询字符串(如果存在))
        /// </summary>
        /// <returns>原始 URL</returns>
        /// <example>http://     www.test.com/aaa/bbb.aspx?id=5&name=kelli
        /// 返回url = /aaa /  bbb.aspx?id=5&name=kelli
        /// </example>
        public static string GetRawUrl()
        {
            string url = HttpContext.Current.Request.RawUrl;
            if (url.StrIsNullOrEmpty())
            {
                return HttpContext.Current.Request.Url.PathAndQuery;
            }
            return url;
        }
        #endregion
        #region 获取当前请求的URL中域名信息后的部分，不包括查询字符串
        /// <summary>
        /// 获取当前请求的URL中域名信息后的部分，不包括查询字符串
        /// </summary>
        /// <returns>文件夹名+页面名</returns>
        /// <example>http://www.test.com/aaa/bbb.aspx?id=5&name=kelli
        /// 返回url= aaa/bbb.aspx
        /// </example>
        public static string GetAbsolutePath()
        {
            string url = HttpContext.Current.Request.Url.AbsolutePath;
            if (url.StrIsNullOrEmpty())
            {
                return HttpContext.Current.Request.Path;
            }
            return url;
        }
        #endregion

        #region 来源地址
        /// <summary>
        /// 来源地址
        /// </summary>
        public static string GetRefererUrl()
        {
            return GetServerString("Http_Referer");
        }
        #endregion

    }
}
